package com.kmproject.haifu.app.utils

import android.app.AlertDialog
import android.graphics.Color
import android.support.annotation.LayoutRes
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import com.blankj.utilcode.util.ScreenUtils
import com.kmproject.haifu.R
import com.kmproject.haifu.mvp.ui.widget.pullRefresh.PullToRefreshView
import org.jetbrains.anko.layoutInflater

fun View.singleClick(l: (android.view.View?) -> Unit) {
    setOnClickListener(l)
}

/**
 * 多控件处理同一点击事件
 */
fun singleClickMoreView(l: (android.view.View?) -> Unit, vararg views: (android.view.View?)) {
    views.forEach {
        it!!.setOnClickListener(l)
    }
}

fun View.longClick(l: View.OnLongClickListener) {
    setOnLongClickListener(l)
}

fun View.visible(show: Boolean = true) {
    this.visibility = if (show) View.VISIBLE else View.GONE
}

fun ViewGroup.inflate(@LayoutRes layoutRes: Int, attachToRoot: Boolean = false): View {
    return context.layoutInflater.inflate(layoutRes, this, attachToRoot)
}

fun ViewGroup.inflate(@LayoutRes layoutRes: Int, root: ViewGroup? = null, attachToRoot: Boolean = false): View {
    return context.layoutInflater.inflate(layoutRes, root, attachToRoot)
}

fun View.showCenterDialog(dialog: AlertDialog, isTouchCancel: Boolean = true) {
    val window = dialog.window
    window?.setGravity(Gravity.CENTER)
    window?.setWindowAnimations(R.style.dialog1)
//    val lp = window.attributes
//    lp.width =
//    lp.height = WindowManager.LayoutParams.WRAP_CONTENT
//    window.setAttributes(lp)
    setBackgroundColor(Color.WHITE)
    dialog.setCanceledOnTouchOutside(isTouchCancel)
    dialog.show()
    dialog.setContentView(this)
    dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    window?.setLayout((ScreenUtils.getScreenWidth() * 0.8f + 0.5).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
}

fun PullToRefreshView.isPullDown(curCount: Int, pageSize: Int): Boolean {
    setmCurCount(curCount)
    setmPageSize(pageSize)
    return isClearData
}